package com.zbu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbu.reggie.common.R;
import com.zbu.reggie.entity.Employee;
import com.zbu.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController//交给IOC容器管理
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /*
    员工登录
    request的作用： 登录成功 之后 把employee的id 存到session一份，request可以获取当前登录对象
    * */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        //1.将页面提交的密码进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的username查询数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        //3.如果没有查到返回失败结果
        if (emp == null) {
            return R.error("登录失败");
        }
        //4.密码是否正确
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        //5.查看员工状态是否禁用
        if (emp.getStatus() == 0) {
            return R.error("账号禁用");
        }
        //6.登录成功将员工id存入session 并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /*
    员工退出
    * */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {

        //1.清理Session中保存的当前登录的员工的id //登录时候的名字
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /*
     * 员工添加*/
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工，员工信息{}", employee.toString());

        //设置时间需要md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //元数据对象处理器 自动处理填充createTime，updateTime，createUser，updateUser
        employeeService.save(employee);//MP
        return R.success("新增员工成功");
    }

    /*
     * 员工信息分页查询*/
    //页面需要的什么 泛型里面写什么， Page里面有页面需要的两个参数
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page:{},pageSize:{},name:{}", page, pageSize, name);
        //分页构造器
        Page pageinfo = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper();
        wrapper.like(!StringUtils.isEmpty(name), Employee::getName, name);
        //排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageinfo, wrapper);
        return R.success(pageinfo);
    }

    /*
    根据id更新员工
    1.修改状态
    2.更新员工
    程序没有错误但是没有更改信息 因为id是long类型 传给js会丢失精度
    用消息转换器 baextendMessageConverters 转成String 可以解决
    * */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("employe:{}", employee.toString());
        long empId = (long) request.getSession().getAttribute("employee");
        //元数据对象处理器 自动处理填充createTime，updateTime，createUser，updateUser
        employeeService.updateById(employee);//MP
        return R.success("修改员工成功");
    }


    //根据ID查询员工 实现回显
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long id) {
        log.info("根据ID查询员工");
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

}
